package com.shem.watermark.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;

/* loaded from: classes2.dex */
public class FillWidthTextView extends AppCompatTextView {
    public FillWidthTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public static Rect a(TextPaint textPaint, String str) {
        Rect rect = new Rect();
        textPaint.getTextBounds(str, 0, str.length(), rect);
        return rect;
    }

    @Override // android.widget.TextView, android.view.View
    public final void onDraw(Canvas canvas) {
        boolean z8;
        boolean z9;
        String charSequence = getText().toString();
        if (!TextUtils.isEmpty(charSequence)) {
            canvas.save();
            int i7 = 0;
            canvas.clipRect(new Rect(getPaddingLeft(), 0, getWidth() - getPaddingRight(), getHeight()));
            int width = (getWidth() - getPaddingLeft()) - getPaddingRight();
            TextPaint paint = getPaint();
            int colorForState = getTextColors().getColorForState(getDrawableState(), 0);
            if (getHint() != null && charSequence.length() == 0 && getHintTextColors() != null) {
                colorForState = getHintTextColors().getColorForState(getDrawableState(), 0);
            }
            paint.setColor(colorForState);
            int height = (a(paint, "中").height() / 2) + (((getHeight() - getPaddingTop()) - getPaddingBottom()) / 2) + getPaddingTop();
            int i9 = 1;
            if (!charSequence.endsWith("：") && !charSequence.endsWith(":")) {
                z8 = false;
            } else {
                z8 = true;
            }
            if (!charSequence.endsWith("：") && !charSequence.endsWith(":")) {
                z9 = false;
            } else {
                z9 = true;
            }
            int i10 = 0;
            int i11 = 0;
            while (true) {
                if (i10 < charSequence.length()) {
                    if (z9 && i10 == charSequence.length() - 2) {
                        i11 += a(getPaint(), charSequence.substring(i10, i10 + 2)).width();
                        break;
                    } else {
                        int i12 = i10 + 1;
                        i11 += a(getPaint(), charSequence.substring(i10, i12)).width();
                        i10 = i12;
                    }
                } else {
                    break;
                }
            }
            int i13 = width - i11;
            int length = charSequence.length();
            if (z8) {
                i9 = 2;
            }
            int i14 = i13 / (length - i9);
            if (i14 < 0) {
                i14 = 0;
            }
            int paddingLeft = getPaddingLeft();
            while (true) {
                if (i7 < charSequence.length()) {
                    if (z8 && i7 == charSequence.length() - 2) {
                        canvas.drawText(charSequence.substring(i7, i7 + 2), paddingLeft, height, paint);
                        break;
                    }
                    int i15 = i7 + 1;
                    String substring = charSequence.substring(i7, i15);
                    canvas.drawText(substring, paddingLeft, height, paint);
                    paddingLeft = a(paint, substring).width() + paddingLeft + i14;
                    i7 = i15;
                } else {
                    break;
                }
            }
            canvas.restore();
        }
    }
}